package com.ubing.boot.notesforjava.test.juc;

import java.io.*;
/**
 * @ClassName : MyClassLoader
 * @Description : MyClassLoader
 * @Author : Xxxxx
 * @Date: 2023-03-08 15:03
 */
public class MyClassLoader extends ClassLoader{
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        File file = new File("C:/Users/高翔/desktop",name.replace(".","/").concat(".class"));

//        src/main/java/com/ubing/boot/notesforjava/test/A.class
        try {
                FileInputStream fis = new FileInputStream(file);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int b=0;
                while ((b=fis.read())!=-1){
                    baos.write(b);
                }
                byte[] bytes = baos.toByteArray();
                for (int i = 0; i < bytes.length; i++) {
                    System.out.println(bytes[i]);
                }
                baos.close();
                fis.close();
                return defineClass("com.", bytes,0, bytes.length);
            } catch (Exception e) {
                e.printStackTrace();
            }
        return super.findClass(name);
    }

    public static void main(String[] args) throws Exception {
        ClassLoader classLoader = new MyClassLoader();
        Class<?> aClass = classLoader.loadClass("A");
        Object o = aClass.newInstance();

    }
}
